//
//  SandBoxStore.m
//  UI17_本地存储数据(sandBox)
//
//  Created by apple on 16/5/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "SandBoxStore.h"

@import UIKit;

@implementation SandBoxStore

#pragma mark - Public Function


+ (instancetype)shareSandBoxStore{

    static SandBoxStore *store = nil;

    if (store == nil) {
        
        store = [[SandBoxStore alloc] init];
        
    }

    return store;

}


#pragma mark - Path
//返回沙盒路径
- (NSString *)PathForSandBox{

    //core API
    return NSHomeDirectory();
    
}

- (NSString *)PathForDocument{

   return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

}

- (NSString *)pathForLibrary{

    return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];

}

- (NSString *)pathForCache{

    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

}

- (NSString *)pathForTmp{

    return NSTemporaryDirectory();

}

- (NSString *)pathForApp{

   return [[NSBundle mainBundle] bundlePath];

}

#pragma mark - Store

- (BOOL)storeFileWithString:(NSString *)content
                     ForKey:(NSString *)key{

    //Core API
    NSString *path = [[self PathForDocument] stringByAppendingPathComponent:key];
    
    return [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    
    
}

- (BOOL)storeFileWithArray:(NSArray *)content
                    Forkey:(NSString *)key{

    NSString *path = [[self PathForDocument] stringByAppendingPathComponent:key];
    NSLog(@"%@",path);
    return [content writeToFile:path atomically:YES];

}

- (BOOL)storeFileWithDictionary:(NSDictionary *)content
                         Forkey:(NSString *)key{

    NSString *path = [[self PathForDocument] stringByAppendingPathComponent:key];
    
    return [content writeToFile:path atomically:YES];

}


- (BOOL)storeFileWithData:(NSData *)content
                   Forkey:(NSString *)key{

    NSString *path = [[self PathForDocument] stringByAppendingPathComponent:key];

    return [content writeToFile:path atomically:YES];

}

- (BOOL)storeFileWithImage:(UIImage *)content
                    Forkey:(NSString *)key{

    //Core API
    NSData *data = UIImageJPEGRepresentation(content, 1.0);

    if (data == nil) {
        
        data = UIImagePNGRepresentation(content);
        
    }
    
    return [self storeFileWithData:data Forkey:key];

}


- (BOOL)storeFileWithImageData:(NSData *)imageData
                        Forkey:(NSString *)key{

    return [self storeFileWithData:imageData Forkey:key];

}

- (BOOL)storeByArchiverWithObject:(id)object
                           Forkey:(NSString *)key{

    NSMutableData *mdata = [NSMutableData data];
    //归档类
    NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:mdata] autorelease];
    
    [archiver encodeObject:object forKey:key];
    //结束归档.
    [archiver finishEncoding];
    
    return [self storeFileWithData:mdata Forkey:key];
    
}

#pragma mark - Read

- (NSString *)readStringForKey:(NSString *)key{

    NSString *path = [[self PathForDocument] stringByAppendingPathComponent:key];

    return [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

}


- (NSArray *)readArrayForKey:(NSString *)key{

    NSString *path = [[self PathForDocument] stringByAppendingPathComponent:key];
    
    return [NSArray arrayWithContentsOfFile:path];

}

- (NSDictionary *)readDictionaryForKey:(NSString *)key{

    NSString *path = [[self PathForDocument] stringByAppendingPathComponent:key];
    
    return [NSDictionary dictionaryWithContentsOfFile:path];

}

- (NSData *)readDataForKey:(NSString *)key{

    NSString *path = [[self PathForDocument] stringByAppendingPathComponent:key];
    
    return [NSData dataWithContentsOfFile:path];

}

- (UIImage *)readImageForKey:(NSString *)key{

    NSData *data = [self readDataForKey:key];

    return [UIImage imageWithData:data];
    
}



- (id)readByUnArchiverWithKey:(NSString *)key{

    NSData *date = [self readDataForKey:key];

    //反归档
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
                                     initForReadingWithData:date];

    id obj = [unarchiver decodeObjectForKey:key];
    
    [unarchiver finishDecoding];
    
    return obj;

}



- (BOOL)createDirectoryToDocument:(NSString *)directory{

   return [self createDirectoryToSuperDirectory:NSDocumentDirectory
                                directory:directory];
    
}


- (BOOL)deleteDirctoryInDocument:(NSString *)directory{

    return [self deleteDirctoryInSuperdirectory:NSDocumentDirectory
                                      directory:directory];

}




#pragma mark - Private Function

- (NSString *)getFilePath:(NSString *)key{

   return [[self PathForDocument] stringByAppendingPathComponent:key];

}


- (BOOL)createDirectoryToSuperDirectory:(NSSearchPathDirectory)superdirectory
                              directory:(NSString *)directory{
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(superdirectory, NSUserDomainMask, YES) lastObject]
                      stringByAppendingPathComponent:directory];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    
     NSLog(@"%@",[self PathForSandBox]);
    //创建文件夹
    //1参: 文件夹路径 , 2参:父目录没有时是否自动创建 , 3参:新目录的文件属性.
    return [manager createDirectoryAtPath:path
       withIntermediateDirectories:YES attributes:nil error:nil];
    
   
}


- (BOOL)deleteDirctoryInSuperdirectory:(NSSearchPathDirectory)Superdirectory
                             directory:(NSString *)directory{
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(Superdirectory, 1, YES) lastObject] stringByAppendingPathComponent:directory];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    
    return [manager removeItemAtPath:path error:nil];
    
    
}





@end
